{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "41d386b0",
   "metadata": {},
   "source": [
    "--------\n",
    "* 2022/10/11/week08\n",
    "* python课堂笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e7a5029",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 0. 代码的注释\n",
    "> 1. 用户输入\n",
    "> 2. 格式化输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f089b5a",
   "metadata": {},
   "source": [
    "## 0.代码的注释\n",
    "\n",
    "> 1.#，一般写在代码块的最上边，或者每一行代码的最后，用于代码思路的记录，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00e0cb1a",
   "metadata": {},
   "outputs": [],
   "source": [
    " # Author:Zhichao\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b7ded7c",
   "metadata": {},
   "source": [
    "> 2.'''注释内容'''  ，一般是注释代码块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4f51ced",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "import random\n",
    "wait_time = random.randint(1,60)\n",
    "print(wait_time)\n",
    "\n",
    "word = \"bottles\"\n",
    " print(word)\n",
    "'''\n",
    "print(\"三个引号注释代码块内容，不运行\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96d85caa",
   "metadata": {},
   "source": [
    "## 1.用户输入\n",
    "> 1. input()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "73238e6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'chaichai'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "username "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "658507f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:chaichai\n",
      "密码:123456\n",
      "chaichai\n",
      "123456\n"
     ]
    }
   ],
   "source": [
    "# 登录系统 \n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "\n",
    "\n",
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5ac1b7bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:chaichai\n",
      "密码:123456\n",
      "欢迎chaichai登录!\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代1\n",
    "# 假设我们数据库有用户信息：账号：chaichai , 密码：123456\n",
    "user1 = \"chaichai\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "# 情景一:输入信息正确，登陆成功\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+user1+\"登录!\")\n",
    "\n",
    "# 情景二：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e1e434ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:jiscbud\n",
      "密码:hauhdue\n",
      "账号不存在或密码不正确\n",
      "您还有2次尝试的机会\n",
      "账号:iqhwhdiq\n",
      "密码:hquowdhqud\n",
      "账号不存在或密码不正确\n",
      "您还有1次尝试的机会\n",
      "账号:chaichai\n",
      "密码:123456\n",
      "欢迎chaichai登录!\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代2循环功能\n",
    "# 假设我们数据库有用户信息：账号：chaichai , 密码：123456\n",
    "user1 = \"chaichai\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1):  # i 是一个int类型\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "    # 情景一:输入信息正确，登陆成功\n",
    "    if user1 == username and password1 == password:\n",
    "        print(\"欢迎\"+user1+\"登录!\")\n",
    "        break #终止循环\n",
    "\n",
    "    # 情景二：账号不存在或密码不正确\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请1小时候再来尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确\")\n",
    "            print(\"您还有\"+str(i-1)+\"次尝试的机会\") #str强制将int转换成str"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d943c66",
   "metadata": {},
   "source": [
    "## 2.格式化输出\n",
    "> 1. 字符串拼接\n",
    "> 2. 占位符\n",
    "> 3. format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1dde205f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:拆拆\n",
      "年龄:19\n",
      "工作:学生\n",
      "收入:2000\n",
      "地区:广州\n",
      "------INFO OF 拆拆------\n",
      "年龄:19\n",
      "工作:学生\n",
      "薪资:2000\n",
      "地区:广州\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法一：字符串拼接\n",
    "name = input(\"姓名:\")\n",
    "age = input(\"年龄:\") \n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info = '''------INFO OF '''+ name +'''------'''+'''\n",
    "年龄:'''+age+'''\n",
    "工作:'''+job+'''\n",
    "薪资:'''+salary+'''\n",
    "地区:'''+reg\n",
    "\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "07a1feef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method raw_input in module ipykernel.kernelbase:\n",
      "\n",
      "raw_input(prompt='') method of ipykernel.ipkernel.IPythonKernel instance\n",
      "    Forward raw_input to frontends\n",
      "    \n",
      "    Raises\n",
      "    ------\n",
      "    StdinNotImplementedError if active frontend doesn't support stdin.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "cc6ea8ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄:19\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# input 作为用户输入，其都以字符串形式返回\n",
    "age = input(\"年龄:\")\n",
    "print(age,type(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b9616fe8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:liz\n",
      "年龄:19\n",
      "工作:学生\n",
      "收入:1800\n",
      "地区:广州\n",
      "------INFO OF liz------\n",
      "年龄: 19\n",
      "工作: 学生\n",
      "薪资: 1800\n",
      "地区: 广州\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法二：占位符 %s ，依次有序输入\n",
    "name = input(\"姓名:\")\n",
    "age = int(input(\"年龄:\"))  # 强制将 str 转换成 int\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info = '''------INFO OF %s------\n",
    "年龄: %d\n",
    "工作: %s\n",
    "薪资: %s\n",
    "地区: %s'''%(name,age,job,salary,reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "26c0b4a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:liz\n",
      "年龄:19\n",
      "工作:学生\n",
      "收入:1800\n",
      "地区:广州\n",
      "------INFO OF liz------\n",
      "年龄: 19\n",
      "工作: 学生\n",
      "薪资: 1800\n",
      "地区: 广州\n"
     ]
    }
   ],
   "source": [
    "name = input(\"姓名:\")\n",
    "age = input(\"年龄:\")\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info = '''------INFO OF %s------\n",
    "年龄: %s\n",
    "工作: %s\n",
    "薪资: %s\n",
    "地区: %s'''%(name,age,job,salary,reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "0708b854",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:沈丹菁\n",
      "年龄:22\n",
      "工作:环境设计师\n",
      "收入:4000000\n",
      "地区:汕头\n",
      "------INFO OF 沈丹菁------\n",
      "姓名：沈丹菁\n",
      "年龄:22\n",
      "工作: 环境设计师\n",
      "薪资: 4000000\n",
      "地区: 汕头\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法三: format() 格式化输出函数\n",
    "name = input(\"姓名:\")\n",
    "age = int(input(\"年龄:\"))  # 强制将 str 转换成 int\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info = '''------INFO OF {姓名}------\n",
    "姓名：{姓名}\n",
    "年龄:{年龄}\n",
    "工作: {工作}\n",
    "薪资: {薪资}\n",
    "地区: {地区}'''.format(年龄 = age,姓名 = name,工作 = job,薪资 = salary,地区 = reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a23e0d76",
   "metadata": {},
   "source": [
    "## 课后练习\n",
    "* 要求：\n",
    "> 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "> 2. 让用户输入商品编号进行商品的购买。\n",
    "> 3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "> 4. 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52b2986c",
   "metadata": {},
   "outputs": [],
   "source": [
    "good_list = [\n",
    "    ('美妆蛋',20),\n",
    "    ('化妆刷',38),\n",
    "    ('花茶',50),\n",
    "    ('抱枕',60),\n",
    "]\n",
    "user_shopping_car = [\n",
    "    \n",
    "]\n",
    "\n",
    "Alipay = int(input(\"请输入你的余额：\"))\n",
    "while True:\n",
    "    print(\"-----请选择编号 欢迎选购-----\")\n",
    "    for i in range(len(good_list)):\n",
    "        print(\"商品编号:%s,商品名称:%s,商品价格:%s\"%(i,goods_list[i][0],good_list[i][1]))\n",
    "             \n",
    "             \n",
    "    choice = input(\"请正确输入购买商品的编号:\")\n",
    "    if choice.isdigit():\n",
    "        choice = int(choice)\n",
    "        if choice > len(goods_list):\n",
    "             print(\"你选择的商品不存在\")\n",
    "             \n",
    "        else:\n",
    "             if Alipay > goods_list[choice][1]:\n",
    "                user_shopping_car.append(goods_list[choice])\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                print(\"【%s】已经加入你的购物车，您的【账户余额】还有:%s元\"%(goods_list[choice][0],Alipay))\n",
    "             else:\n",
    "                print(\"您的余额不足，请更换支付方式\")\n",
    "             \n",
    "    elif choice == \"q\":\n",
    "         print(\"用户购物车:\",user_shopping_car)\n",
    "         print(\"您的余额还有:\",Alipay)\n",
    "         break\n",
    "    else:\n",
    "         print(\"请输入商品编号^^\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "872a6c28",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
